package com.iad.controller.command
{
  import com.adobe.cairngorm.commands.Command;
  import com.adobe.cairngorm.control.CairngormEvent;
  import com.iad.controller.event.*;
  import com.iad.controller.manager.*;
  import com.iad.controller.service.*;
  import com.iad.model.*;
  import com.iad.model.valueobject.*;
  import com.iad.view.component.*;
  import com.iad.view.form.*;
  import com.iad.view.util.ascb.util.*;
  
  import mx.collections.ArrayCollection;
  import mx.managers.CursorManager;
  
  public class SignupCommand implements Command
  {
    [Bindable]
    private var model:ModelLocatorImpl = ModelLocatorImpl.getInstance();
    
 
    public function execute(event:CairngormEvent):void {
      switch (event.type) {
        case SignupEvent.SEND_CONTACT_MESSAGE:
          sendContactMessage(event as SignupEvent);
        break;
        case SignupEvent.SEND_WELCOME_EMAIL:
          sendWelcomeEmail(event as SignupEvent);
        break;
     }
    }
    
    
    private function sendWelcomeEmail(event:SignupEvent):void {
      var service:SignupService = new SignupService();
      service.addEventListener(ServiceResultEvent.SEND_WELCOME_EMAIL_RESULT, sendWelcomeEmailOnResult, false, 0, false);
      service.addEventListener(ServiceErrorEvent.ERROR, onError, false, 0, true);
      service.sendWelcomeEmail(event.object as WelcomeEmail);
    }
    
    protected function sendWelcomeEmailOnResult(event:ServiceResultEvent):void {
      trace("SiteCommand::sendWelcomeEmailOnResult()");
    }
    
    private function sendContactMessage(event:SignupEvent):void {
      var service:SignupService = new SignupService();
      service.addEventListener(ServiceResultEvent.SEND_CONTACT_MESSAGE_RESULT, sendContactMessageOnResult, false, 0, false);
      service.addEventListener(ServiceErrorEvent.ERROR, onError, false, 0, true);
      service.sendContactMessage(event.object as ContactMessage);
    }
    
    protected function sendContactMessageOnResult(event:ServiceResultEvent):void {
      trace("SiteCommand::sendContactMessageOnResult()");
    }
    
    private function onError(event:ServiceResultEvent):void {
      CursorManager.removeBusyCursor();
      trace("error: " + event);
    }
     
  } 
}
    